package com.mobile.service.impl;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.WorkOrderConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.WorkOrderDTO;
import com.mobile.service.dto.WorkOrderQueryDTO;
import com.mobile.service.entity.WorkOrder;
import com.mobile.service.inter.WorkOrderService;
import com.mobile.service.mapper.WorkOrderMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 工单表service实现类
 */
@Service("workOrderService")
@Transactional
public class WorkOrderServiceImpl implements WorkOrderService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private WorkOrderConverter workOrderConverter;
    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Override
    public Map<String, Object> addWorkOrder(WorkOrderDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        workOrderMapper.insertWorkOrder(workOrderConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageResult<WorkOrderDTO> getWorkOrderListPage(WorkOrderQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<WorkOrderDTO> list = workOrderMapper.workOrderlistPage(queryDTO);
        return new PageResult<WorkOrderDTO>(list);
    }

    @Override
    public WorkOrderDTO getWorkOrderById(Integer id) throws Exception {
        return workOrderMapper.getWorkOrderById(id);
    }

    @Override
    public Map<String, Object> updateWorkOrder(WorkOrderDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Integer pkValue = dto.getWorkOrderId();
        if (pkValue == null) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        WorkOrderDTO data = this.getWorkOrderById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        workOrderMapper.updateWorkOrder(workOrderConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        return map;
    }

    @Override
    public Map<String, Object> removeWorkOrder(Integer id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        workOrderMapper.deleteWorkOrder(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<WorkOrderDTO> getWorkOrderList(WorkOrderQueryDTO queryDTO) throws Exception {
        return workOrderMapper.getWorkOrderList(queryDTO);
    }

    @Override
    public WorkOrder getWorkOrderEntityById(Integer id) throws Exception {
        WorkOrderDTO dto = workOrderMapper.getWorkOrderById(id);
        if (dto == null) {
            return null;
        }
        return workOrderConverter.toEntity(dto);
    }

    @Override
    public void updateWorkOrder(WorkOrder workOrder) throws Exception {
        workOrderMapper.updateWorkOrder(workOrder);
    }

    @Override
    public WorkOrderDTO getWorkOrderStatistics() {
        return workOrderMapper.getWorkOrderStatistics();
    }
}
